package org.me.app.student;

import java.util.ArrayList;

import org.me.app.R;
import org.me.app.instructor.AnnListActivity;
import org.me.app.instructor.PostAnnouncementActivity;
import org.me.bean.Announcement;
import org.me.bean.CourseSchedule;
import org.me.bean.Student;


import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class AnnouncementListActivity extends ListActivity{
	
	private String courseScheduleId = new String();
	private ArrayList<Announcement> annList = new ArrayList<Announcement>();
	private Student student = new Student();
	private ArrayAdapter<Announcement> annAdapter;
	private Announcement ann = new Announcement();
	
	private static final int POST_ANNOUNCMENT_DIALOG = 1;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Bundle b = getIntent().getExtras();
		student = b.getParcelable("Student");
		annList = b.getParcelableArrayList("AnnList");
		courseScheduleId = b.getString("CourseScheduleId");
		
		if(annList.isEmpty()){
			Toast.makeText(getApplicationContext(), 
					"There are no announcements availiable yet.", 
					Toast.LENGTH_SHORT)
					.show();
		}
		
		annAdapter= new ArrayAdapter<Announcement>(getApplicationContext(), 
				R.layout.courseinfor, 
				annList);
		
		setListAdapter(annAdapter);
		
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				ann = annList.get(position);
				showDialog(POST_ANNOUNCMENT_DIALOG);
			}

		});
		
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case POST_ANNOUNCMENT_DIALOG:
			return new AlertDialog.Builder(AnnouncementListActivity.this)
					.setIcon(R.drawable.icon)
					.setTitle(ann.getTitle())
					.setMessage(ann.getContent())
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {

								}
							})
					.setNeutralButton("some thing",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {

									/* User clicked Something so do some stuff */
								}
							})
					.setNegativeButton("Cancel",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {

									/* User clicked Cancel so do some stuff */
								}
							}).create();
		}
		
		return null;
	}
	
	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		// TODO Auto-generated method stub
		super.onPrepareDialog(id, dialog);
		switch (id) {
		case (POST_ANNOUNCMENT_DIALOG):
			AlertDialog annDialog = (AlertDialog) dialog;
			annDialog.setTitle(ann.getTitle());
			annDialog.setMessage(ann.getContent());

			break;
		

		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		CreateMenu(menu);
		return true;
	}



	private void CreateMenu(Menu menu) {
		menu.setQwertyMode(true);
		MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
		{
			mnu1.setAlphabeticShortcut('p');
			mnu1.setIcon(R.drawable.icon);
		}
		MenuItem mnu2 = menu.add(0, 1, 1, "Refresh");
		{
			mnu2.setAlphabeticShortcut('r');
			mnu2.setIcon(R.drawable.icon);
		}

	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return MenuChoice(item);
	}
	
	private boolean MenuChoice(MenuItem item) {
		switch (item.getItemId()) {
		case 0:

			Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
			return true;
		case 1:
			refreshAnnouncements();
			return true;

		}
		return false;
	}

	private void refreshAnnouncements() {
		// annList.clear();
		org.me.services.WSlib ws = new org.me.services.WSlib();
		annList = (ArrayList<Announcement>) ws
				.refreshAnnouncementList(courseScheduleId);
		annAdapter.notifyDataSetChanged();
		annAdapter = new ArrayAdapter<org.me.bean.Announcement>(
				getApplicationContext(), R.layout.courseinfor, annList);

		setListAdapter(annAdapter);

	}

}
